package com.brainshare.utils;

import cn.hutool.crypto.digest.MD5;
import com.alibaba.fastjson.JSON;
import com.brainshare.common.utils.http.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

public class RedPacketUtils
{
    public static String API_KEY = "brainshare";
    public static String UID = "10814023";

    protected static final Logger logger = LoggerFactory.getLogger(RedPacketUtils.class);

    /**
     * 发红包
     *
     * @param orderId 本次提现红包的订单Id
     * @param money   红包金额
     * @param openId  第三方提现接口的授权OpenId，通过前端授权跳转获得
     * @return
     */
    public static RedPacketResult sendRedPacket(String orderId, String money, String openId)
    {
        String type = "1";
        String reqtick = String.valueOf(new Date().getTime() / 1000);
        //参数验证签名 md5(uid+type+orderid+money+regtick+openid+apikey)
        String sign = MD5.create().digestHex(UID + type + orderId + money + reqtick + openId + API_KEY);
        String title = "红包筛查裂变，提现已开启！";
        String sendname = "筛查端裂变版";
        String wishing = "恭喜您成功提现，祝您心想事成，好运连连！";
        String redpacketUrl = "https://www.yaoyaola.net/exapi/SendRedPackToOpenid?uid=" + UID + "&type=" + type + "&orderid=" + orderId + "&money=" + money + "&reqtick=" + reqtick + "&openid=" + openId + "&sign=" + sign + "&title=" + title + "&sendname=" + sendname + "&wishing=" + wishing;
        logger.info("提现接口发送请求参数" + redpacketUrl);
        String result = HttpClientUtil.doGet(redpacketUrl);
        RedPacketResult redPacketResult = JSON.parseObject(result, RedPacketResult.class);
        redPacketResult.setRequestMsg(redpacketUrl);
        logger.info("提现接口接收请求参数" + JSON.toJSONString(redPacketResult));
        return redPacketResult;
    }

    /**
     * 根据红包订单ID和
     *
     * @param orderId  红包订单ID
     * @param ticketId 红包第三方流水号
     * @return
     */
    public static RedPacketResult queryRedPacket(String orderId, String ticketId)
    {
        String queryRedPacketUrl = "https://www.yaoyaola.net/exapi/checkhb?uid=" + UID + "&orderid=" + orderId + "&ticket=" + ticketId;
        String result = HttpClientUtil.doGet(queryRedPacketUrl);
        RedPacketResult redPacketResult = JSON.parseObject(result, RedPacketResult.class);
        return redPacketResult;
    }

    public static RedPacketResult queryRedPacketAccount()
    {
        String queryRedPacketUrl = "https://www.yaoyaola.net/exapi/accountinfo?uid=" + UID;
        String result = HttpClientUtil.doGet(queryRedPacketUrl);
        RedPacketResult redPacketResult = JSON.parseObject(result, RedPacketResult.class);
        return redPacketResult;
    }

}
